# -*- coding=UTF-8 -*- 

#This script put all the config files into memory. In other words, it is used to initialize.
#Usage: 
#   from init import init
#   device_list,scenario_match,scenario_list,statue_list = init()
#   
#   Then you will get four things:
#   (Description)
#   1.scenario_list:{
#   'S1001': {'Action': ['D01A01,0A,1'], 
#     'Scenario': '\xe6\x89\x93\xe5\xbc\x80\xe4\xb8\x80\xe6\xa5\xbc\xe5\x8d\xab\xe7\x94\x9f\xe9\x97\xb4\xe5\x86\x85\xe5\x90\x8a\xe9\xa1\xb6\xe7\x81\xaf'}, 
#   'S1003': {'Action': ['D01A02,0A,1', 'D01A02,0A,2', 'D02A02,0A,1', 'D02A01,address,circuit'], 
#       'Scenario': '\xe4\xb8\x80\xe6\xa5\xbc\xe8\xbf\x9b\xe5\x85\xa5\xe4\xbc\x91\xe7\x9c\xa0\xe6\xa8\xa1\xe5\xbc\x8f'}, 
#   'S1002': {'Action': ['D01A01,0A,2'], 
#           'Scenario': '\xe6\x89\x93\xe5\xbc\x80\xe4\xb8\x80\xe6\xa5\xbc\xe5\x8d\xab\xe7\x94\x9f\xe9\x97\xb4\xe6\x8e\x92\xe9\xa3\x8e\xe6\x89\x87'}}
#   Here are all scenario s
# 
#   2.scenario_match=
#       '55001A010100': ['S1001'],
#       '56001A020100': ['S1002'], 
#       '55001A020100': ['S1002'], 
#       '56001A010100': ['S1001'], 
#       '5500[..]08[..]00': ['S1003', 'address', 'circuit']}
#   command->scenario match
#
#   3.device_list={
#       'D02': 
#           {'Function': {'D02A08': ['AE[..]0[.]08DF', 'address', 'circuit'], 
#                         'D02A05': ['AE[..]0[.][.]5DF', 'address', 'circuit', 'second'], 
#                         'D02A04': ['AE[..]0[.][.]4DF', 'address', 'circuit', 'second'], 
#                         'D02A07': ['AE[..]2[.][..]DF', 'address', 'circuit', 'minute'], 
#                         'D02A06': ['AE[..]1[.][..]DF', 'address', 'circuit', 'second'], 
#                         'D02A01': ['AE[..]0[.]01DF', 'address', 'circuit'], 
#                         'D02A02': ['AE[..]0[.]02DF', 'address', 'circuit']},
#           'Address': ['0A', '0B']},
#       'D01': 
#           {'Function': {'D01A08': ['AF[..]0[.]08DF', 'address', 'circuit'], 
#               'D01A01': ['AF[..]0[.]01DF', 'address', 'circuit'], 
#               'D01A02': ['AF[..]0[.]02DF', 'address', 'circuit']
#            'Note': '\xe8\xa5\xbf\xe5\x8d\x97\xe6\x99\xba\xe8\x83\xbd\xe7\xa7\x91\xe6\x8a\x80', 
#            'Address': ['0A', '0B'], '\xef\xbc\x88\xef\xbc\x9f\xef\xbc\x89
#            'Statue': '[0,0,0,0,0]',
#            'CircuitNumber': '5',
#            'Type': '\xe4\xba\x94\xe8\xb7\xaf\xe7\xbb\xa7\xe7\x94\xb5\xe5\x99\xa8'}}
#   here are all devices
# 
#   4.statue_list=
#        {'D02': None, 'D01': '[0,0,0,0,0]'}
#   here are all statue of devices. This is mainly used for future visualization.



def get_device_info():
    device_config=open('device.cfg','r')
    device_text=device_config.readlines()
    now_device=None

    device_info={}
    address_list=[]
    function_list={}
    
    
    for line in device_text:
        line=line.strip()
        sharp_loc=line.find('#')
        if sharp_loc>-1: line=line[:sharp_loc].strip()
                
        if '</' in line:
            if now_device==None : raise RuntimeError
            device_info['Address']=address_list
            device_info['Function']=function_list
            
            device_list[now_device]=device_info
            now_device=None

        elif '<' in line:
            now_device=line.strip().strip('<').strip('>')
            address_list=[]
            function_list={}
            device_info={}

        elif '=' in line:
            p=line.split('=')
            if p[0].strip()=='Address':
                address_list.append(p[1].strip())
            elif p[0].strip()=='Function':
                
                function_name=p[1].split(',')[0]
                function_list[function_name]=p[1].split(',')[1:]
                
            else: device_info[p[0]]=p[1]
            



    
def get_scenario_list():
    '''This function initializes the scenario_list, then the result
       dictionary scenario_match is used for command_match'''
    scenario_config=open('scenario.cfg','r')
    scenario_text=scenario_config.readlines()
    now_scenario=None
    
    scenario_info={}
    action_list=[]
    for line in scenario_text:
        line=line.strip()
        if '</' in line:
            if now_scenario==None: raise RuntimeError
            
            scenario_info['Action']=action_list
            scenario_list[now_scenario]=scenario_info
            
            now_scenario=None
            
        elif '<' in line:
            if now_scenario!=None: raise RuntimeError
            now_scenario=line.strip().strip('<').strip('>')
            action_list=[]
            scenario_info={}
            
        elif '=' in line:
            p=line.split('=')
        
            if p[0].strip()=='Command':
                right_content=line.split('=')[1]
                command=right_content.split(',')[0]
                parameter=right_content.split(',')[1:] 
                scenario_match[command]=[now_scenario]+parameter

            if p[0].strip()=='Scenario':
                scenario_info['Scenario']=p[1].strip()

            if p[0].strip()=='Action':
                action_list.append(p[1].strip())
            
    # print '\n\n\n\nscenario command match:\n', scenario_match
    # print '\n\n\nscenario_list:\n',scenario_list
    # print '\n\n\ncommand:\n',scenario_match.keys()

def get_statue():
    for device in device_list.keys():
        statue_list[device]=device_list[device].get('Statue',None)


##command_list=
##{'AF[..]0[.][.]DF';('D01A01','address','circuit','second'),
##'AF[..]0[.][.]DF';('D01A02','address','circuit','second')}

device_list={}
scenario_match={}
scenario_list={}
statue_list={}


def init():
    import time
    print 'Now initializing......'
    start=time.time()
    get_scenario_list()
    get_device_info()
    get_statue()

    
    print 'Initialization complete successfully'
    print 'Time used:',time.time()-start
    return device_list,scenario_match,scenario_list,statue_list

if __name__ == '__main__':
    device_list,scenario_match,scenario_list,statue_list = init()
    print '\nscenario command match:\n', scenario_match
    print '\nscenario_list:\n',scenario_list
    print '\ncommand:\n\n',scenario_match.keys()
    print '\ndevice list:\n',device_list
    print '\nstatue list:\n',statue_list